package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;

import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.result.Result;
import com.itheima.mp.service.UserService;


import com.itheima.mp.domain.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户模块
 */
@RestController
@RequestMapping("/user/user")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 新增用户信息
     *
     * @param userFormDTO
     */
    @PostMapping("/add")
    public void add(@RequestBody UserFormDTO userFormDTO) {
        User user = new User();
        BeanUtils.copyProperties(userFormDTO, user);
        userService.save(user);
    }

    /**
     * 根据用户id删除用户
     *
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Long id) {
        userService.removeById(id);
    }

    /**
     * 根据用户id获取用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/select/{id}")
    public Result<User> select(@PathVariable Long id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 根据用户ids批量查
     *
     * @param ids
     * @return
     */
    @GetMapping
    public Result<List<UserVO>> list(@RequestParam List<Long> ids) {
        List<User> users = userService.listByIds(ids);
//        List<UserVO> userVOs = new ArrayList<>();
//        for (User user : users) {
//            UserVO userVO1 = new UserVO();
//            BeanUtils.copyProperties(user, userVO1);
//            userVOs.add(userVO1);
//        }

        return Result.success(BeanUtil.copyToList(users, UserVO.class));

    }

    /**
     * 修改用户账余额
     *
     * @param id
     * @param money
     */
    @PutMapping("/user/{id}/deduction/{money}")
    public void deduction(@PathVariable Long id, @PathVariable Integer money) {
        Map<String, Object> map = new HashMap<>();
        map.put("money", money);
        map.put("id", id);
        userService.updateMoney(map);
    }

    /**
     * 根据条件查询特定条件下的用户列表信息
     *
     * @param query
     * @return
     */
    @GetMapping("/list")
    public Result<List<UserVO>> list(UserQuery query) {
        List<User> users = userService.queryUserList(query);
        List<UserVO> userVOS = BeanUtil.copyToList(users, UserVO.class);
        return Result.success(userVOS);
    }

    /**
     * 查询所有的用户列表
     *
     * @return
     */
    @GetMapping("/allList")
    public Result<List<UserVO>> allList() {
        List<User> list = userService.list();
        List<UserVO> userVOS = BeanUtil.copyToList(list, UserVO.class);
        return Result.success(userVOS);
    }

    /**
     * 根据用户id查询用户以及地址列表
     *
     * @param id
     * @return
     */

    @GetMapping("/list/{id}")
    public Result<UserVO> list(@PathVariable Long id) {
        UserVO userVO = userService.getUserAndAddrList(id);
        return Result.success(userVO);
    }

    /**
     * 根据用户们的ids批量查询到用户信息和地址列表
     *
     * @param ids
     * @return
     */
    @GetMapping("/list/detailList")
    public Result<List<UserVO>> listByIds(@RequestParam List<Long> ids) {
        List<UserVO> userVOS = userService.getUserAndAddrListByIds(ids);
        return Result.success(userVOS);


    }

    @GetMapping("/page")
    public Result<PageDTO<UserVO>> page(UserQuery query) {
        PageDTO<UserVO> pageDTO = userService.getPageCondition(query);
        return Result.success(pageDTO);
    }
}
